/* 
 * File:   SockEndPoint.h
 * Author: max
 *
 * Created on May 22, 2013, 11:05 PM
 */

#ifndef SOCKENDPOINT_H
#define	SOCKENDPOINT_H

#include <stdint.h>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "common_ProtocoloConstantes.h"
#include "common_Excepcion.h"

using namespace std;

namespace TP {

class SockEndPoint {

public:
    
    SockEndPoint(int &pFileDescriptor, struct sockaddr_in* &pClientAddress);
    
    SockEndPoint(int &pFileDescriptor, struct sockaddr_in* &pClientAddress, const int &pTimeout);
    
    void Finalizar ();
    
    void Escribir (const uint8_t* pData, const size_t &pLongitudData) throw (Excepcion&);
    
    void Leer (uint8_t* &pData, uint32_t &pLongitudData) throw (Excepcion&);
    
    virtual ~SockEndPoint();
    
private:
    
    void decodificarError (string &pMensaje, int& pCodigoError);
    
    void EscribirBuffer (const uint8_t* pData, const uint32_t pLongitudData) throw (Excepcion&);
    
    void inicializarBuffer (uint8_t* &buffer, size_t &longitudBuffer)throw (Excepcion&) ;
    
    uint8_t getByteDesdeSocket() throw (Excepcion&);
    
    void desplazarIzquierdaBuffer (uint8_t* pBuffer, const size_t &pLongitudBuffer, uint32_t pVeces);
    
    void redimensionarBuffer (uint8_t* &pBuffer, size_t &pLongitudBuffer);

    int _fileDescriptor;
    struct sockaddr_in* _clientAddress;
    uint32_t _timeOut;
};

}
#endif	/* SOCKENDPOINT_H */

